<?php
namespace app\index\controller;
use think\Controller;
use think\Session;
class Sys extends Mycontroller
{
    public function __construct()
    {
        Parent::__construct();
        // $classList = db('class')->select();
        // $this->assign('classList',$classList);

    }
    public function index()
    {
    }
    public function gosectionnew()
    {
        return $this->fetch('sys/sectionnew');
    }
    public function gointernew()
    {
        return $this->fetch('sys/internew');
    }
    public function internew()
    {
        //判定数据不为空
        foreach($_POST as $v){
            if(empty($v)){
                echo  "<script>alert('信息不能为空');history.back()</script>";
                return ;
            }
        }
        //判断时间格式
        if(strtotime($_POST['starttime'])&&strtotime($_POST['endtime'])){
            //判断 结束时间>开始时间 &&结束时间>现在时间
            if(!(strtotime($_POST['endtime'])>strtotime($_POST['starttime']))&&(strtotime($_POST['endtime'])>time())){
                echo  "<script>alert('结束时间格式错误');history.back()</script>";
                return ;
            }
        }else{
            echo  "<script>alert('时间格式不正确');history.back()</script>";
            return ;
        }
        // 转化为时间戳格式
        $_POST['starttime']=strtotime($_POST['starttime']);
        $_POST['endtime']=strtotime($_POST['endtime']);
        $res= db('sys')->insert($_POST);
        // echo  "<script>alert('发送成功')</script>";
        // return  action('UserIndex/index');
        if($res){
            return $this->success('发送成功','UserIndex/index');
        }else{
            echo  "<script>alert('发送失败');history.back()</script>";
            return ;
        }
    }


    public function goonenew()
    {
        return $this->fetch('sys/onenew');
    }
    public function onenew()
    {
        //判定数据不为空
        if(empty($_POST['getid'])||empty($_POST['content'])){
            echo  "<script>alert('数据不能为空');history.back()</script>";
            return ;
        }
        //生成数据 插入数据库
        $data = [
            'sendid'=>$_POST['sendid'],
            'getid'=>$_POST['getid'],
            'newsname'=>$_POST['newsname'],
            'content'=>$_POST['content'],
            'sendtime'=>time(),
            'status'=>0
        ];
        $res= db('news')->insert($data);
        if($res){
        return $this->success('发送成功','UserIndex/index');
        }else{
            echo  "<script>alert('发送失败');history.back()</script>";
            return ;
        }
    }


    public function readsys($id)
    {
        //在关联表中查询数据
        $res = db('revsys')->where('sid','=',$id)->where('uid','=',Session::get('userid'))->find();
        if($res){
            $data = [
            'id'=>$res['id'],
            'uid' => Session::get('userid'),
            'sid' => $id,
            'status' => '2'
            ];
            $result = db('revsys')->update($data);

        }else{
            $data = [
            'uid' => Session::get('userid'),
            'sid' => $id,
            'status' => '2'
            ];
            $result = db('revsys')->insert($data);
        // var_dump($result,$res);die; 
        }
        if($result){
            echo  "<script>alert('删除成功');</script>";
            return  action('UserIndex/Index');
        }else{
            echo  "<script>alert('操作失败');</script>";
            return  action('UserIndex/Index');
        }
    }

    public function readnews($id)
    {
        //找到id 将状态改为
        $data = [
            'id'=>$id,
            'status'=>2
        ];
        $res = db('news')->update($data);
        if($res){
            echo  "<script>alert('操作成功');</script>";
            return  action('UserIndex/Index');
        }else{
            echo  "<script>alert('操作失败');</script>";
            return  action('UserIndex/Index');
        }
    }
}